<?php

namespace App\Listeners;

use App\Events\AuthStepEvent;
use App\Models\UserAuthStepEventLog;

class AuthStepEventListener {
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct() {
        //
    }

    /**
     * Handle the event.
     *
     * @param AuthStepEvent $event
     * @return void
     */
    public function handle(AuthStepEvent $event) {
        //
        $post = $event->post;

        $data = [];
        if (is_array($post)) {
            $data = [
                'uid'             => $post['uid'],
                'step_id'         => $post['step_id'],
                'loan_request_id' => $post['loan_request_id'],
                'type'            => $post['type'],
                'is_current'      => UserAuthStepEventLog::CURRENT_STEP_YES
            ];
        }
        try {
            $step = UserAuthStepEventLog::byLoanId($post['loan_request_id'])->get();
            foreach ($step as $item) {
                $item->is_current = UserAuthStepEventLog::CURRENT_STEP_NO;
                $item->save();
            }
            UserAuthStepEventLog::create($data);
        } catch (\Exception $e) {
            \Log::debug('执行认证步骤异常：' . $e->getMessage());
        }
    }
}